import { ajax ,cardCache, toast, promptBox} from 'nc-lightapp-front';
import { DOCLIST } from '../../../../common/constant';
import { calcTotal } from '../../../../common/util/calcUtils';
import { byPkorgFiter, fiterFrkm } from '../../../../common/util/fiterUtils';
import { FIELD, CARD_ADD_DISABLED_BUTTON, CARD,REQUEST_URL } from "../../constant";

let {setDefData, getDefData } = cardCache;
/**
* @description: 表头编辑后事件
* @param: moduleId 区域编码
* @param: key 当前字段编码
*/
export function afterHeadEvent(props, moduleId, key, value, oldValue) {
        
        let meta = props.meta.getMeta();
        if('dbilldate' == key){
                let pk_org = props.form.getFormItemsValue(moduleId, 'pk_org').value;
                let pk_accbook = getDefData("accbook","accbook");
               
                let data = {
                        editKey : key,
                        data : [
                                {
                                        key : key,
                                        value : value.value
                                },
                                {
                                        key : 'pk_org',
                                        value : pk_org
                                },
                                {
                                        key : 'pk_accbook',
                                        value : pk_accbook
                                }
                        ]
                }

                javaAfterHeadEvent(props, data);

        }else if('ass' == key){//会计科目编辑后

                setAssRequiredFiled(props, moduleId);
               
                let pk_assoa = props.form.getFormItemsValue(moduleId, 'ass').value;//会计科目
                let pk_assdept = props.form.getFormItemsValue(moduleId, 'assdept').value;//部门

                if(pk_assoa == null){
                        props.form.setFormItemsRequired(moduleId,{ 'vouchertype': { value: null, display: null }});
                }



                let data = {
                        editKey : key,
                        data : [
                                {
                                        key : 'assdept',
                                        value : pk_assdept
                                },
                                {
                                        key : 'ass',
                                        value : pk_assoa
                                }
                        ]
                }

                javaAfterHeadEvent(props, data);

        }else if (key == 'accbank') {//银行账户
                let pk_org = props.form.getFormItemsValue(moduleId, 'pk_org').value;
                let value = props.form.getFormItemsValue(CARD.form_id, 'accbank');
                let pk_assoa = props.form.getFormItemsValue(moduleId, 'ass').value;
                if(value.value == ''){
                        props.form.setFormItemsDisabled(moduleId, {assdept:false});
                }else{
                        props.form.setFormItemsDisabled(moduleId, {assdept:true});
                }

                let data = {
                        editKey : key,
                        data : [
                                {
                                        key : key,
                                        value : value.value
                                },
                                {
                                        key : 'pk_org',
                                        value : pk_org
                                },
                                {
                                        key : 'ass',
                                        value : pk_assoa
                                }
                        ]
                }

                javaAfterHeadEvent(props, data);

        }else if(key == 'assdept'){//部门编辑后

                // props.form.setFormItemsValue(CARD.form_id, { 'center': { value: value.refpk, display: value.refname } });

                let pk_assoa = props.form.getFormItemsValue(moduleId, 'ass').value;
                let pk_assdept = props.form.getFormItemsValue(moduleId, 'assdept').value;
                if(pk_assoa == null){
                        props.form.setFormItemsRequired(moduleId,{ 'vouchertype': { value: null, display: null }});
                }
                let data = {
                        editKey : key,
                        data : [
                                {
                                        key : 'assdept',
                                        value : pk_assdept
                                },
                                {
                                        key : 'ass',
                                        value : pk_assoa
                                }
                        ]
                }

                javaAfterHeadEvent(props, data);
        }else if (key == FIELD.org) {

                if(oldValue.value != ''){
                        
                        let head_keys = [];
                        let body_keys = ['tax','money'];
                        promptBox({
                                color: 'warning',
                                title: "组织变更",
                                content: '更换组织将会清空界面信息',
                                noFooter: false,
                                noCancelBtn: true,
                                beSureBtnClick: ()=>{
                                        let headfields = ['psn','asspsn','assdept','asscutsup','banktype','accbank','vouchertype','center','ass','verifyno','def1','pk_notetype','def2','def7','def8','def9','def10','def11'];
                                        clearSomeFields(props, headfields);
        
                                        let value = calcTotal(props, CARD.form_id, head_keys, CARD.table_code, body_keys);
                                        props.form.setFormItemsValue(CARD.form_id, { 'count': { value: value} });
                                }
                        })
                }
                
                props.resMetaAfterPkorgEdit(); //选择主组织以后，恢复其他字段的编辑性
                props.button.setButtonDisabled(CARD_ADD_DISABLED_BUTTON, false);//恢复增行编辑性
                props.cardTable.setAllCheckboxAble(CARD.table_code, true);
                
                filterByOrg(props, moduleId);
        }
}

function javaAfterHeadEvent(props, data){
        ajax({
                url : REQUEST_URL.afterHeadEvent,
                data : data,
                success: (res) => {
                        let items = res.data.items;
                        items.map((item) => {
                                //由java返回的字段控制
                                props.form.setFormItemsValue(CARD.form_id, { [item.key]: item.value })
                        })

                },
                error:(res) =>{
                        toast({content:res.message, color:'danger'});//错误消息

                        props.form.setFormItemsValue(CARD.form_id, {[data.editKey]: {value : '', display: ''}});
                }
                
        })
}

export function setAssRequiredFiled(props, moduleId){

        let data = props.createMasterChildData(CARD.page_code, CARD.form_id, CARD.table_code, 'cardTable');

        ajax({
                url : REQUEST_URL.assRequiredFiled,
                data : data,
                success: (res) => {
                        props.form.setFormItemsRequired(moduleId, res.data);

                }
        })
}

export function filterByOrg(props, moduleId){
        let data = null;
        let value = props.form.getFormItemsValue(moduleId, 'pk_org');
        if(value.value != ''){
                data = value;
        }

        let pk_org = value.value;

        if(data != null){
                ajax({
                        url: REQUEST_URL.getOrgMap,
                        data: data,
                        success: (res) => {
                                        if(res.data.accbook!=null){
                                        setDefData("accbook","accbook",res.data.accbook);//表体科目过滤使用

                                        let key = 'ass';
                                        let pk_accountingbook= getDefData("accbook","accbook");
                                        let pk_org = props.form.getFormItemsValue('card_head', 'pk_org').value;
                                        let fiterType = DOCLIST.qtsrjf;
                                        fiterFrkm(props, moduleId, key, pk_org, pk_accountingbook, fiterType);
                                }
                        }
                });

                byPkorgFiter(props, moduleId, 'assdept', pk_org, false);//部门
                byPkorgFiter(props, moduleId, 'center', pk_org, false);//利润中心
                byPkorgFiter(props, moduleId, 'asspsn', pk_org, false);//人员
                byPkorgFiter(props, moduleId, 'psn', pk_org, false);//报销人
                byPkorgFiter(props, moduleId, 'accbank', pk_org, false);//银行账户
                byPkorgFiter(props, moduleId, 'asscutsup', pk_org, false);//客商
                byPkorgFiter(props, moduleId, 'def7', pk_org, false);//项目工程名称
                
        }
}

function clearSomeFields(props, headfields){

        // if(bodyfields != null){
        //     for(let i = 0; i < bodyfields.length; i++) {
        //         props.cardTable.setValByKeyAndIndex(CARD.table_code,index,bodyfields[i],{ value : "", display : ""});
        //     }
        // }
        let count = props.cardTable.getAllRows(CARD.table_code).length;
        for(let i = 0; i < count; i ++){
                props.cardTable.delRowsByIndex(CARD.table_code, [0]);
        }
        props.cardTable.setTableData(CARD.table_code, []);
        
        if(headfields != null){
            for(let i = 0; i < headfields.length; i++) {
                let value = new Object;
                value[headfields[i]] = { value : "", display : ""};
                props.form.setFormItemsValue(CARD.form_id, value);
            }
        }
    }